{include file='header'/}
<body>
<div class="weadmin-body">
    <form class="layui-form" action="javascript:;" method="post" onsubmit="return dosub(this)">
        <fieldset class="layui-elem-field layui-field-title site-title">
            <legend><a name="hr">{$modelname}</a></legend>
        </fieldset>
        <table class="layui-table">
            <tbody>
            <tr>
                <td><span class="we-red">*</span>字段类型</td>
                <td>
                    <select name="fieldtype" id="fieldtype" lay-filter="fieldtype_mode" lay-verify="required" class="select" onchange="javascript:field_setting(this.value);">
                        <option value="" selected>请选择字段类型</option>
                        <option value="input">单行文本</option>
                        <option value="textarea">多行文本</option>
                        <option value="number">整数类型</option>
                        <option value="datetime">时间类型</option>
                        <option value="image">图片上传</option>
                        <option value="images">多图片上传</option>
                        <option value="attachment">附件上传</option>
                        <option value="select">option下拉框</option>
                        <option value="radio">radio选项卡</option>
                        <option value="checkbox">checkbox多选框</option>
                        <option value="editor">编辑器</option>
                        <option value="editor_mini">编辑器(Mini版)</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td><span class="we-red">*</span>字段名</td>
                <td><input type="text" class="layui-input" name="field" lay-verify="required|Vfield" onblur="check_field()" id="field" placeholder="只能由英文字母、数字和下划线组成，且数字不能开头，例如：name">
                </td>
            </tr>
            <tr>
                <td><span class="we-red">*</span>字段别名</td>
                <td>
                    <input type="text" class="layui-input" name="name" lay-verify="required" id="name" placeholder="例如：姓名">
                </td>
            </tr>
            <tr>
                <td>字段提示</td>
                <td>
                    <input type="text" class="layui-input" name="tips" id="tips" placeholder="显示在文本框里作为表单输入提示文字">
                </td>
            </tr>
            <tr class="fieldtype_mode mode_datetime" style="display:none">
                <td>时间格式</td>
                <td>
                    <input type="radio" name="dateset" value="0" title="日期（{php}echo date('Y-m-d',time()){/php}）" checked>
                    <input type="radio" name="dateset" value="1" title="日期+时间（{php}echo date('Y-m-d H:i:s',time()){/php}）">
                </td>
            </tr>
            <tr class="fieldtype_mode mode_select mode_radio mode_checkbox">
                <td>选项列表</td>
                <td>
                    <textarea name="setting" id="textarea" class="layui-textarea" placeholder="用“|”分开，如“男|女|人妖”"></textarea>
                </td>
            </tr>
            <tr>
                <td>默认值</td>
                <td>
                    <input type="text" class="layui-input" name="defaultvalue">
                </td>
            </tr>
            <tr>
                <td>字符长度取值范围</td>
                <td>
                    <div class="layui-inline">
                        <label class="layui-form-label" style="width:auto">最小值</label>
                        <div class="layui-input-inline">
                            <input type="number" name="minlength" autocomplete="off" class="layui-input" value="0">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" style="width:auto">最大值</label>
                        <div class="layui-input-inline">
                            <input type="number" name="maxlength" autocomplete="off" class="layui-input" value="100">
                        </div>
                    </div>
                </td>
            </tr>
            <tr>
                <td>数据校验未通过的提示信息</td>
                <td>
                    <input type="text" class="layui-input" name="errortips">
                </td>
            </tr>
            <tr>
                <td>是否启用</td>
                <td>
                    <input type="radio" name="disabled" value="0" title="启用" checked>
                    <input type="radio" name="disabled" value="1" title="禁用" >
                </td>
            </tr>
            </tbody>
        </table>

        <div class="layui-form-item">
            <label class="layui-form-label"></label>
            <input type="hidden" name="dosubmit" value="1">
            <input type="hidden" name="modelid" value="{:input('modelid')}">
            <button class="layui-btn" lay-submit="" type="submit" name="dosubmit">确定</button>
        </div>
    </form>
</div>

{include file='footer'/}

<script>
    layui.use(['form'], function(){
        var $ = layui.jquery,form = layui.form;
            form.verify({
            Vfield: [
                /^[a-zA-Z]{1}([a-zA-Z0-9]|[_]){0,19}$/
                ,'配置名称必须为英文拼音、数字或者下划线_！且数字不能开头'
              ]
        });
        form.on('select(fieldtype_mode)', function(data){
            $('.fieldtype_mode').hide();
            $('.mode_'+ data.value).show();
        });
    });
    function check_field(){
        var res = false;
        var reg = /^[a-zA-Z]{1}([a-zA-Z0-9]|[_]){0,19}$/;
        if(!reg.test($("input[name='field']").val())){
            layer.msg('字段名不正确！',{icon:2});
            return false;
        }
        $.ajax({
            type: "post",
            url: "{:url('public_check_field')}",
            dataType: "html",
            async:false,
            data: "field="+$('#field').val()+"&modelid={:input('modelid')}",
            success: function(msg){
                if(msg){
                    if(msg==0){
                        layer.msg('字段名已经存在！',{icon:2});
                    }else if(msg==1){
                        res = true;
                    }else{
                        layer.alert('Ajax通信失败！');
                    }
                }
            }
        });
        return res;
    }
    function check_name(){
        if($("input[name='name']").val()==''){
            layer.msg('字段别名不能为空！');
            return false;
        }else{
            return true;
        }
    }
    function dosub(obj) {
        if(!check_field() || !check_name()){
            return false;
        }

        $.ajax({
            type: 'POST',
            url: '{:url('add')}',
            data: $(obj).serialize(),
            dataType: "json",
            success: function (res) {
                if (res.status == 1) {
                    layer.msg(res.msg, {
                        icon: 1,
                        time: 200
                    }, function () {
                        parent.location.reload();
                    });
                } else {
                    layer.msg(res.msg);
                }
            }
        });
        return false;
    }
</script>
</body>
</html>